using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]

public class ParallaxLayer
{
    [SerializeField] private Transform background;
    [SerializeField] private float parallaxMultiplier;  
    [SerializeField] private float imageWidthOffset = 10;

    private float imageFullWidth;
    private float imageHalfWidth;

    public void CalculateImageWidth()
    {
        imageFullWidth = background.GetComponent<SpriteRenderer>().bounds.size.x;
        imageHalfWidth = imageFullWidth / 2;
    }


    public void Move(float distanceMove)
    {
        background.position += Vector3.right * (distanceMove * parallaxMultiplier);
    }

    public void LoopBackground(float cameraLeftEdge,float cameraRightEdge)
    {
        float imageRightEdge =(background.position.x +imageHalfWidth)-imageWidthOffset;
        float imageLeftEdge =(background.position.x -imageHalfWidth)+ imageWidthOffset;

        if (imageRightEdge < cameraLeftEdge)
            background.position += Vector3.right * imageFullWidth;
        else if(imageLeftEdge > cameraRightEdge)
            background.position += Vector3.right * -imageFullWidth;
    }

}
